/*************************************************************************
	> File Name: ex4.c
	> Author: hua
	> Mail: hua@163.com 
	> Created Time: 2018年02月11日 星期日 15时00分51秒
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"


struct student
{
	char name[10];
	int age;
	float score;
};


int main()
{
	char *buf = "{\"name\":\"mike\",\"age\":23,\"score\":75.2}";
	cJSON *json = NULL;

        puts(buf);
	struct student stu;
	memset(&stu,0,sizeof(stu));

	json = cJSON_Parse(buf);

	cJSON *node = NULL;

	node = cJSON_GetObjectItem(json,"name");
	if(node == NULL)
	{
		printf("can't find\n");
		exit(-1);
	}

	if(node->type == cJSON_String)
	{
		//printf("%s\n",node->valuestring);
		strcpy(stu.name,node->valuestring);
	}


	node = cJSON_GetObjectItem(json,"age");
	if(node == NULL)
	{
		printf("can't find\n");
		exit(-1);
	}
	if(node->type == cJSON_Number)
	{
		//printf("%d\n",node->valueint);
		stu.age = node->valueint;
	}


	node = cJSON_GetObjectItem(json,"score");
	if(node == NULL)
	{
		printf("can't find\n");
		exit(-1);
	}
	
	if(node->type == cJSON_Number)
	{
		printf("%lf\n",node->valuedouble);
		stu.score = node->valuedouble;
	}


	printf("name is %s age is %d score is %.1f\n",stu.name,stu.age,stu.score);

	cJSON_Delete(json);


	return 0;
}
